package com.bolun998300.personal_blog_springboot.service;

import com.bolun998300.personal_blog_springboot.dao.BlogTypeDao;
import com.bolun998300.personal_blog_springboot.pojo.BlogType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BlogTypeService {

    @Autowired
    private BlogTypeDao blogTypeDao;


    public Page<BlogType> listByPage(int page,int size){
        Sort sort = new Sort(Sort.Direction.ASC,"orderNo");
        Pageable pageable = new PageRequest(page-1,size,sort);
        Page<BlogType> pageFromJPA = blogTypeDao.findAll(pageable);
        return pageFromJPA;
    }

    public List<BlogType> getAll(){
        return blogTypeDao.findAllByOrderByOrderNo();
    }

    public void add(BlogType blogType){
        blogTypeDao.save(blogType);
    }

    public void update(BlogType blogType){
        blogTypeDao.save(blogType);
    }

    public void delete(int id){
        blogTypeDao.delete(id);

    }

    public BlogType get(int id){
        return blogTypeDao.findOne(id);
    }

}
